<?php
namespace app\controllers;

use Yii;
use app\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;

class UserController extends Controller {
    // 用户列表页
    public function actionIndex() {
        $users = User::find()->all();
        
        return $this->render('index', [
            'users' => $users,
        ]);
    }
    
    // 用户详情页
    public function actionView($id) {
        $user = $this->findModel($id);
        
        return $this->render('view', [
            'user' => $user,
        ]);
    }
    
    // 创建用户
    public function actionCreate() {
        $model = new User();
        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
        
        return $this->render('create', [
            'model' => $model,
        ]);
    }
    
    // 查找模型
    protected function findModel($id) {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }
        
        throw new NotFoundHttpException('请求的页面不存在。');
    }
}
?>